package org.syncsoft.overalt.gui.impl.window.commitFiles;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractWindowController;

/**
 * 
 * @author tptfc
 *
 */
public class CommitFilesWindowController extends AbstractWindowController<CommitFilesWindow> {
	private Collection<String> filePaths;
	
	public CommitFilesWindowController(Collection<String> filePaths) {
		setFilePaths(filePaths);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.window.AbstractWindowController#doCreateWindow()
	 */
	@Override
	protected CommitFilesWindow doCreateWindow() throws SystemException {
		CommitFilesWindow window = new CommitFilesWindow(this);
		
		Iterator<String> i = getFilePaths().iterator();
		while (i.hasNext()) {
			String filePath = i.next();
			
			try {
				getCore().getFileTitle(filePath.trim());
			} catch (SystemException e) {
				i.remove();
			}
		}
		
		window.setFilePaths(getFilePaths());
		return window;
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.window.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	protected void doSubmit(WindowContextValue context) throws SystemException {
		String versionDescription = context.getValue("versionDescription");
		Collection<String> filePaths = context.getValues("filePaths");
		
		getCore().commit(filePaths, versionDescription);
	}

	
	//	GETs AND SETs
	private Collection<String> getFilePaths() {
		return filePaths;
	}
	private void setFilePaths(Collection<String> filePaths) {
		this.filePaths = filePaths;
	}

	public static void main(String[] args) {
		Collection<String> paths = new HashSet<String>();
		paths.add("/home/tptfc/Developer/java/syncsoft/amigos-dropbox-fork/a.txt");
		paths.add("/home/tptfc/Developer/java/syncsoft/amigos-dropbox-fork/b.txt");
		
		CommitFilesWindowController c = new CommitFilesWindowController(paths);
		c.start();
	}
}
